const fs = require('fs');//?文件夹操作

let server = function (app, db) {

    // const web_information=fs.readFileSync('./html/webinformation.html');//?本站信息

    //?Cookie查询
    const View_Cookie = function (request, calka) {
        db.query('select * from admin_login where AdminCookie=\'' + request.cookies.DOM_Cookie_admin + '\'', function (err, data) {
            calka(err, data)
        })
    }

    //?管理页面
    app.get('/admin/:htmlN', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                console.log(request.params.htmlN);
                fs.readFile('./admin_html/' + request.params.htmlN, function (err, data1) {
                    if (err) console.error(err);
                    response.header({ "content-type": "text/html" })
                    response.send(data1)
                })
            } else response.send({ "code": 0, "data": fs.readFileSync('./html/Login.html') })
        })
    })

    //?菜单页面
    app.get('/admin/user_detail.html/:UserID', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                console.log(request.params.htmlN);
                fs.readFile('./admin_html/user_detail.html', function (err, data1) {
                    if (err) console.error(err);
                    response.header({ "content-type": "text/html" })
                    response.send(data1.toString().replace(`id="user_id" data-userid=""`, `id="user_id" data-userid="${request.params.UserID}"`))
                })
            } else response.send({ "code": 0, "data": fs.readFileSync('./html/Login.html') })
        })
    })
}
module.exports = server
